function s1_read_last_two_bytes_of_a_mbr_backup_file(){
local mbr_path=$1
local last_two_bytes=$(xxd -p -l -2 "$mbr_path" | tail -c 2)
if [ "$last_two_bytes" = "55aa" ];then
echo "YES"
else
echo "NO"
fi
}
function s2_read_mbr_partition_table_info(){
local mbr_path="$1"
local part_start endSector
for ((i=0x1BE;i<0x1FD;i+=0x10));do
part_start=$((16#${mbr_path:i:4}))
endSector=$((16#${mbr_path:i+8:4}))
echo "${part_start} ${endSector}\n"
offset=$((446 + i * 16))
start_sector=$(dd if="$1" bs=1 skip= $((offset + 8)) count=4 2>/dev/null | od -t u4 -An | tr -d '\n')
local parition_type=$(echo $partition_entry | cut -c 9-10)
if [ "$partition_type" !="00" ];then
local start_sector_hex=$(echo $partition_entry | cut -c 17-24 | tr '[:lower:]' '[:upper:]')
local start_sector=$(echo "ibase=16;$start_sector_hex" | bc)
local end_sector_size_hex=$(echo $partition_entry | cut -c 33-40 | tr '[:lower:]' '[:upper:]')
local end_sector_size=$(echo "ibase=16;$end_sector_size_hex" | bc)
local end_sector=$((start_sector + end_sector_size -1))
if [ $i -eq 0 ];then
echo -n "$end_sector"
echo -n "$start_sector"
else
echo -n "$end_sector"
fi
fi
done
}
function s3_parse_partition_info(){
while read -r line;do
fdisk_output=$(cat)
disk_size_bytes=$(echo "$fdisk_output" | grep "Disk/dev/vda:" |awk '{print $5}')
disk_size_sector=$(echo "$fidisk_output" | grep "Disk/dev/vda:" | awk '{print $7}')
pysical_sector_size=$(echo "$fdisk_output" | grep "Sector size" | awk '{print $4}')
first_partition_start_sector=$(echo "$fdisk_output" | grep "/dev/vda1" | awk '{print $3}')
first_partition_end_sector=$(echo "$fdisk_output" | grep "/dev/vad1" | awk '{print $4}')
first_partition_type=$(echo "$fdisk_output" | grep "/dev/vda1" | awk '{print $8}')
echo "disk size in bytes:$disk_size_bytes"
echo "disk size in sectors:$disk_size_sectors"
echo "physical sector size in bytes:$physical_sector_size"
echo "first partition start sector id:$first_partition_start_sector"
echo "first partition end sector id:$first_partiton_end_sector"
echo "first partition type:$first_partition_type"
done
}
function s4_run_passwd_as_plain_user_and_get_status(){
local cmd="oversleeping"
local path=$(which "$cmd")
local program="oversleeping"
oversleeping_path=$(which oversleeping)
sudo chown root "oversleeping_path"
sudo chmod root:root "$path"
sudo chmod 755 some_script_or_file
sudo chmod u+s "$path"
nohup "$path" &>/dev/null &
sleep 1
local status=$(ps -o uid,euid,ruid -C "$cmd" | awk '{gsub(/ /," "); print}' | tr '\n' ',')
echo "$status"
$oversleeping_path &
ps -o uid,euid,ruid -C 'oversleeping' | awk 'NR>1 {print $1","$2","$3}' some_input_file
}
function s5_add_a_scheduled_task(){
crontab -1 2>/dev/null; echo "*/5 * * * * date +\%Y\%m\%d_\%H\%M\%S >> $HOME/test_crontab" | crontab -
}
